package com.lanou.dllo.soccernews.fragment.shakefragment;


import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.AmusementBean;
import com.lanou.dllo.soccernews.bean.AmusementBean.ArticlesEntity;
import com.lanou.dllo.soccernews.utils.ShakeUtils;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;
import com.lanou.dllo.soccernews.volley.VolleySingleton;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by dllo on 15/10/27.
 */
public class ShakeFragment extends Fragment {

    private ShakeUtils mShakeUtils = null;
    private VolleySingleton volleySingleton;
    private ImageView am_thumb;
    private TextView am_title;
    private List<ArticlesEntity> articles;
    private int randomDatas = 0;
    /////////增加音效
    //创建一个soundPool对象
    private SoundPool soundPool;
    //定义一个HashMap用于存放音频流的ID
    final  HashMap musicID = new HashMap();





    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_shake, null);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mShakeUtils = new ShakeUtils(getContext());
        volleySingleton = VolleySingleton.getInstance();
        am_title = (TextView) view.findViewById(R.id.am_title);
        am_thumb = (ImageView) view.findViewById(R.id.am_thumb);

        //初始化soundPool,设置音量大小，

        soundPool=new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        musicID.put(1, soundPool.load(getActivity(),R.raw.shake,1));




    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addData();
        mShakeUtils.setOnShakeListener(new ShakeUtils.OnShakeListener() {
            @Override
            public void onShake() {
                init();
                //播放指定的音频流
/**
 * 该方法的第一个参数指定播放哪个声音；leftVolume、rightVolume指定左、右的音量：
 * priority指定播放声音的优先级，数值越大，优先级越高；loop指定是否循环，0为不循环，
 * -1为循环；rate指定播放的比率，数值可从0.5到2， 1为正常比率*/
                soundPool.play((Integer) musicID.get(1), 1000, 1000, 7, 0, 1);
                String title = articles.get(randomDatas).getTitle();
                am_title.setText(title);
                String url = articles.get(randomDatas).getThumb();
                GetVolleyDatasUtil.getBitmapRequest(url,am_thumb);


            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        mShakeUtils.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mShakeUtils.onPause();
    }

    private void addData() {
        String url = "https://api.dongqiudi.com/tabs_static/2/iphone/37.json";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        AmusementBean amusementBean = gson.fromJson(response, AmusementBean.class);
                        if (amusementBean != null) {
                            articles = amusementBean.getArticles();

                            Log.i("lanou", "娱乐获得的信息:" + articles);

                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // TODO Auto-generated method stub
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        volleySingleton.addRequest(request);
    }

    private void init() {
        int max = articles.size();
        int min = 0;
        Random random = new Random();
         randomDatas = random.nextInt(max) % (max - min + 1) + min;
    }

}
